<?php

/**
 * This is the model class for table "Book".
 *
 * The followings are the available columns in table 'Book':
 * @property integer $idbook
 * @property string $name
 * @property string $description
 * @property string $author
 * @property integer $check
 * @property string $createddate
 * @property integer $idstaff
 * @property integer $iduser
 * @property string $resource
 * @property string $urlimage
 * @property integer $downloadbookcount
 * @property integer $ratepoint
 * @property integer $rateclick
 * @property double $point
 *
 * The followings are the available model relations:
 * @property User $iduser0
 * @property Genre[] $genres
 */
class Book extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Book the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'Book';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name', 'required'),
			array('check, idstaff, iduser, downloadbookcount, ratepoint, rateclick', 'numerical', 'integerOnly'=>true),
			array('point', 'numerical'),
			array('name', 'length', 'max'=>50),
			array('author', 'length', 'max'=>100),
			array('resource, urlimage', 'length', 'max'=>500),
			array('createddate,description', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('idbook, name, description, author, check, createddate, idstaff, iduser, resource, urlimage, downloadbookcount, ratepoint, rateclick, point', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'bookGenres' => array(self::HAS_MANY, 'BookGenre', 'idbook'),
			'genres'=>array(self::MANY_MANY, 'Genre'/*Classname*/,
                /*Table(Pricolumn,Pricolumn)*/'book_genre(idbook, idgenre)'),
			'user'=>array(self::BELONGS_TO, 'User', 'iduser')
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'idbook' => 'Idbook',
			'name' => 'Name',
			'description' => 'Description',
			'author' => 'Author',
			'check' => 'Check',
			'createddate' => 'Createddate',
			'idstaff' => 'Idstaff',
			'iduser' => 'Iduser',
			'resource' => 'Resource',
			'urlimage' => 'Urlimage',
			'downloadbookcount' => 'Downloadbookcount',
			'ratepoint' => 'Ratepoint',
			'rateclick' => 'Rateclick',
			'point' => 'Point',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('idbook',$this->idbook);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('author',$this->author,true);
		$criteria->compare('check',$this->check);
		$criteria->compare('createddate',$this->createddate,true);
		$criteria->compare('idstaff',$this->idstaff);
		$criteria->compare('iduser',$this->iduser);
		$criteria->compare('resource',$this->resource,true);
		$criteria->compare('urlimage',$this->urlimage,true);
		$criteria->compare('downloadbookcount',$this->downloadbookcount);
		$criteria->compare('ratepoint',$this->ratepoint);
		$criteria->compare('rateclick',$this->rateclick);
		$criteria->compare('point',$this->point);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function genre()
	{
		$dataProvider= new CActiveDataProvider(Genre::model(),
				array(
				'criteria'=>array(
					'condition'=>'idgenre in (select idgenre from book_genre where idbook = '.$this->idbook.')',
					),
				));
		$genres = $dataProvider->getData();
		$ng = '';
		$c = 0;
		foreach($genres as $genre){
			if($c==0){
			$ng = $genre->name;
			$c++;
			}
			else{
			$ng = $ng.','.$genre->name;
			}
		}
		return $ng;
	}
	public function point(){
		if($this->rateclick==0){ return $this->ratepoint;}
		else 
		$a = $this->ratepoint;
		$b = $this->rateclick;
		return $a/$b;
	}
}